package com.wondersgroup.esclient.common;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

import io.netty.util.internal.StringUtil;

public class Constants {

	/**
	 * ES与Java类型映射关系
	 */
	public static Map<Class<?>, String> typeMapping = Constants.initTypeMapping();

	/**
	 * 换行符
	 */
	public static String NEW_LINE = System.getProperty("line.separator");

	/**
	 * 系统默认配置的分词器
	 */
	public static String DEFAULT_ANALYZER = "standard";

	/**
	 * ES配置文件
	 */
	public static ResourceBundle ESProperties = initESProperties();

	/**
	 * 日期默认格式
	 */
	public static String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis";

	private static Map<Class<?>, String> initTypeMapping() {
		Map<Class<?>, String> typeMapping = new HashMap<Class<?>, String>();
		typeMapping.put(String.class, "text");
		typeMapping.put(Date.class, "date");
		// integer
		typeMapping.put(int.class, "integer");
		typeMapping.put(Integer.class, "integer");
		// long
		typeMapping.put(long.class, "long");
		typeMapping.put(Long.class, "long");
		// short
		typeMapping.put(short.class, "short");
		typeMapping.put(Short.class, "short");
		// byte
		typeMapping.put(byte.class, "byte");
		typeMapping.put(Byte.class, "byte");
		// double
		typeMapping.put(double.class, "double");
		typeMapping.put(Double.class, "double");
		// float
		typeMapping.put(float.class, "float");
		typeMapping.put(Float.class, "float");
		// boolean
		typeMapping.put(boolean.class, "boolean");
		typeMapping.put(Boolean.class, "boolean");

		return typeMapping;
	}

	/**
	 * 初始化配置参数
	 * 
	 * @return
	 */
	private static ResourceBundle initESProperties() {
		ResourceBundle resource = ResourceBundle.getBundle("elasticsearch");
		DEFAULT_ANALYZER = resource.getString("elasticsearch.default.analyzer");
		return resource;
	}

	/**
	 * 没有映射关系,默认为string
	 * 
	 * @param clazz
	 * @return
	 */
	public static String getMappingTyep(Class<?> clazz) {
		String type = typeMapping.get(clazz);
		return StringUtil.isNullOrEmpty(type) ? "string" : type;
	}

}
